﻿using ECGadmin.WebUI.CustomHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using ECGadmin.WebUI.Models;
using ECGadmin.DAL;
using ECGadmin.Common;
using ECGadmin.WebUI.Attributes;
namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class ProductFilterController : Controller
    {
        //
        // GET: /ProductFilter/
        string PathXml = "~/ECGfilterconfig.xml";
        public ActionResult ConfigFilter()
        {
            XDocument xmlDoc = XDocument.Load(Server.MapPath(PathXml));
            var iTemNode = from x in xmlDoc.Descendants("rows") select x;

            var model = new ConfigFilterModel();

            foreach (var item in iTemNode)
            {
                model.activePricRange = Convert.ToBoolean(item.Element("activePricRange").Value);
                model.activeProvider = Convert.ToBoolean(item.Element("activeProvider").Value);
                model.activeColor = Convert.ToBoolean(item.Element("activeColor").Value);
                model.activeSize = Convert.ToBoolean(item.Element("activeSize").Value);
                model.activeRate = Convert.ToBoolean(item.Element("activeRate").Value);
                model.activeAttr = Convert.ToBoolean(item.Element("activeAttr").Value);

                model.priceRangeMin = Convert.ToDecimal(item.Element("priceRangeMin").Value);
                model.priceRangeMinDefault = Convert.ToDecimal(item.Element("priceRangeMinDefault").Value);
                model.priceRangeMax = Convert.ToDecimal(item.Element("priceRangeMax").Value);
                model.priceRangeMaxDefault = Convert.ToDecimal(item.Element("priceRangeMaxDefault").Value);

                model.rangeNumber = Convert.ToInt32(item.Element("rangeNumber").Value);

                model.applyTypePriceRange = item.Element("applyTypePriceRange").Value;
                model.applyTypeProvider = item.Element("applyTypeProvider").Value;
                model.applyTypeColor = item.Element("applyTypeColor").Value;
                model.applyTypeSize = item.Element("applyTypeSize").Value;
                model.applyTypeRate = item.Element("applyTypeRate").Value;
                model.applyTypeAttr = item.Element("applyTypeAttr").Value;

                model.CategoriesPriceRange = item.Element("CategoriesPriceRange").Value;
                model.CategoriesProvider = item.Element("CategoriesProvider").Value;
                model.CategoriesColor = item.Element("CategoriesColor").Value;
                model.CategoriesSize = item.Element("CategoriesSize").Value;
                model.CategoriesRate = item.Element("CategoriesRate").Value;
                model.CategoriesAttr = item.Element("CategoriesAttr").Value;

                model.showNumberLineProvider = Convert.ToInt32(item.Element("showNumberLineProvider").Value);
                model.showNumberLineColor = Convert.ToInt32(item.Element("showNumberLineColor").Value);
                model.showNumberLineSize = Convert.ToInt32(item.Element("showNumberLineSize").Value);
                model.showNumberLineAttr = Convert.ToInt32(item.Element("showNumberLineAttr").Value);

                model.ShowNameProvider = Convert.ToBoolean(item.Element("ShowNameProvider").Value);
                model.ShowNameColor = Convert.ToBoolean(item.Element("ShowNameColor").Value);
            }

            ViewBag.ListStatus = CommonHelpers.OptionFilterProduct();
            // danh muc san pham
            ViewBag.menuList = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ConfigFilter(ConfigFilterModel model)
        {
            XDocument xmlDoc = XDocument.Load(Server.MapPath(PathXml));
            var iTemNode = from x in xmlDoc.Descendants("rows") select x;
            // new{
            //    active = x.Element("active").Value,
            //    priceMin = x.Element("priceMin").Value,
            //    priceMinDefault = x.Element("priceMinDefault").Value,
            //    priceMax = x.Element("priceMax").Value,
            //    priceMaxDefault = x.Element("priceMaxDefault").Value,
            //    rangeNumber = x.Element("rangeNumber").Value,
            //    applyType = x.Element("applyType").Value,
            //    Categories = x.Element("Categories").Value
            //};
            var av = iTemNode.FirstOrDefault();
            av.SetElementValue("activePricRange", model.activePricRange);
            av.SetElementValue("priceRangeMin", model.priceRangeMin);
            av.SetElementValue("priceRangeMinDefault", model.priceRangeMinDefault);
            av.SetElementValue("priceRangeMax", model.priceRangeMax);
            av.SetElementValue("priceRangeMaxDefault", model.priceRangeMaxDefault);
            av.SetElementValue("rangeNumber", model.rangeNumber);
            av.SetElementValue("applyTypePriceRange", model.applyTypePriceRange);
            av.SetElementValue("CategoriesPriceRange", model.CategoriesPriceRange);

            av.SetElementValue("activeProvider", model.activeProvider);
            av.SetElementValue("showNumberLineProvider", model.showNumberLineProvider);
            av.SetElementValue("ShowNameProvider", model.ShowNameProvider);
            av.SetElementValue("applyTypeProvider", model.applyTypeProvider);
            av.SetElementValue("CategoriesProvider", model.CategoriesProvider);

            av.SetElementValue("activeColor", model.activeColor);
            av.SetElementValue("showNumberLineColor", model.showNumberLineColor);
            av.SetElementValue("ShowNameColor", model.ShowNameColor);
            av.SetElementValue("applyTypeColor", model.applyTypeColor);
            av.SetElementValue("CategoriesColor", model.CategoriesColor);

            av.SetElementValue("activeSize", model.activeSize);
            av.SetElementValue("showNumberLineSize", model.showNumberLineSize);
            av.SetElementValue("applyTypeSize", model.applyTypeSize);
            av.SetElementValue("CategoriesSize", model.CategoriesSize);

            av.SetElementValue("activeRate", model.activeRate);
            av.SetElementValue("applyTypeRate", model.applyTypeRate);
            av.SetElementValue("CategoriesRate", model.CategoriesRate);

            av.SetElementValue("activeAttr", model.activeAttr);
            av.SetElementValue("showNumberLineAttr", model.showNumberLineAttr);
            av.SetElementValue("applyTypeAttr", model.applyTypeAttr);
            av.SetElementValue("CategoriesAttr", model.CategoriesAttr);
            

            xmlDoc.Save(Server.MapPath(PathXml));
            return RedirectToAction("ConfigFilter", "ProductFilter");
        }


    }
}
